package com.smile.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.filter.SaFirewallCheckFilterForServlet;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.gps.CoordinateTransformUtil;
import com.smile.commons.gps.PlanarAirlineService;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.*;
import com.smile.model.dto.request.GetAirlineByIdRequestDTO;
import com.smile.model.request.GetAirlineByIdRequest;
import com.smile.model.response.GetAirlineByAirlineCodeResponse;
import com.smile.model.response.GetAirlineByIdResponse;
import com.smile.model.response.ZbzhResponse;
import com.smile.model.response.response.*;
import com.smile.model.request.*;
import com.smile.model.response.GetAirLineListResponse;
import com.smile.service.AirlineService;
import com.smile.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/airline")
public class AirlineController {

    @Autowired
    private ModelService modelService;
    @Autowired
    private AirlineService airlineService;
    @Autowired
    private SaFirewallCheckFilterForServlet saFirewallCheckFilterForServlet;
    private AddPlanarAirlineRequestDTO addPlanarAirlineRequestDTO;

    public static void main(String[] args) {
        AddAirlineRequest airlineRequest = new AddAirlineRequest();
        AddWayPointRequest wayPointRequest = new AddWayPointRequest();
        wayPointRequest.setHeight(new BigDecimal("100"));
        List<AddWayPointRequest> wayPointRequests = new ArrayList<>();
        wayPointRequests.add(wayPointRequest);
        airlineRequest.setWayPoints(wayPointRequests);
        AddAirlineRequestDTO addAirlineRequestDTO = BeanUtil.copyProperties(airlineRequest, AddAirlineRequestDTO.class);
        System.out.println(addAirlineRequestDTO);
    }

    /**
     * 添加航线
     *
     * @param addAirlineRequest
     * @return
     */
//    @SaCheckPermission("航线管理")
    @PostMapping("/add")
    public R<Void> addAirline(@RequestBody @Validated AddAirlineRequest addAirlineRequest) {

        Boolean result = airlineService.addAirline(BeanUtil.copyProperties(addAirlineRequest, AddAirlineRequestDTO.class));
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    /**
     * 添加航线名称
     *
     * @param addAirlineRequest
     * @return
     */
//    @SaCheckPermission("航线管理")
    @PostMapping("/addName")
    public R<Map> addAirlineName(@RequestBody AddAirlineRequest addAirlineRequest) {

        Long result = airlineService.addAirlineName(BeanUtil.copyProperties(addAirlineRequest, AddAirlineRequestDTO.class));
        if (ObjectUtil.isNotNull(result)) {
            Map<String,Object> map = new HashMap();
            map.put("id", result);
            return R.ok(map);
        } else {
            return R.fail("名称已存在");
        }
    }


    /**
     * 添加航线名称
     *
     * @param addAirlineRequest
     * @return
     */
//    @SaCheckPermission("航线管理")
    @PostMapping("/editName")
    public R<Void> editAirlineName(@RequestBody EditAirlineRequest addAirlineRequest) {

        Boolean result = airlineService.editAirlineName(BeanUtil.copyProperties(addAirlineRequest, EditAirlineRequestDTO.class));
        if (result) {
            return R.ok();
        } else {
            return R.fail("名称已存在");
        }
    }



    @PostMapping("/addPlanarAirline")
    public R<Void> addPlanarAirline(@RequestBody AddPlanarAirlineRequest addPlanarAirlineRequest) throws IOException {
        AddPlanarAirlineRequestDTO addPlanarAirlineRequestDTO = BeanUtil.copyProperties(addPlanarAirlineRequest, AddPlanarAirlineRequestDTO.class);
        airlineService.addPlanarAirline(addPlanarAirlineRequestDTO);
        return R.ok();
    }


    @PostMapping("/updatePlanarAirline")
    public R<Void> updatePlanarAirline(@RequestBody AddPlanarAirlineRequest addPlanarAirlineRequest) throws IOException {
        AddPlanarAirlineRequestDTO addPlanarAirlineRequestDTO = BeanUtil.copyProperties(addPlanarAirlineRequest, AddPlanarAirlineRequestDTO.class);
        airlineService.updatePlanarAirline(addPlanarAirlineRequestDTO);
        return R.ok();
    }
    //生成面状航线
    @PostMapping("/generatorPlanarAirline")
    public R<GetPlanarAirlineResultResponseDTO> GeneratorPlanarAirline(@RequestBody GeneratorPlanarAirlineRequestDTO generatorPlanarAirlineRequestDTO) throws IOException {
//        AddPlanarAirlineRequestDTO addPlanarAirlineRequestDTO = BeanUtil.copyProperties(addPlanarAirlineRequest, AddPlanarAirlineRequestDTO.class);
        GetPlanarAirlineResultResponseDTO result = airlineService.generatorPlanarAirline(generatorPlanarAirlineRequestDTO);
        return R.ok(result);
    }

    /**
     * 获取航线列表
     *
     * @param getAirlineListRequest
     * @return
     */
    @PostMapping("/getAirlineList")
    public R<Page<GetAirLineListResponse>> getAirlineList(@RequestBody GetAirlineListRequest getAirlineListRequest) {
        GetAirlineListRequestDTO getAirlineListRequestDTO = BeanUtil.copyProperties(getAirlineListRequest, GetAirlineListRequestDTO.class);
        Page<GetAirLineListResponseDTO> pages = airlineService.getAirlineList(getAirlineListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(pages, GetAirLineListResponse.class));
    }

    /**
     * 编辑航线
     *
     * @param editAirlineRequest
     * @return
     */
//    @SaCheckPermission("航线管理")
    @PostMapping("/editAirline")
    public R<Void> editAirline(@Validated @RequestBody EditAirlineRequest editAirlineRequest) {
        EditAirlineRequestDTO editAirlineRequestDTO = BeanUtil.copyProperties(editAirlineRequest, EditAirlineRequestDTO.class);
        Boolean result = airlineService.editAirline(editAirlineRequestDTO);
        if (result) {
            return R.ok();

        } else {
            return R.fail();
        }
    }

    /**
     * 根据id获取航线
     * @param getAirlineByIdRequest
     * @return
     */
    @PostMapping("/getAirlineById")
    public R<GetAirlineByIdResponse> getAirlineById(@Validated @RequestBody GetAirlineByIdRequest getAirlineByIdRequest) {
        GetAirlineByIdRequestDTO getAirlineByIdRequestDTO = BeanUtil.copyProperties(getAirlineByIdRequest, GetAirlineByIdRequestDTO.class);
        GetAirlineByIdResponseDTO getAirlineByIdResponseDTO = airlineService.getAirlineById(getAirlineByIdRequestDTO);
        return R.ok(BeanUtil.copyProperties(getAirlineByIdResponseDTO, GetAirlineByIdResponse.class));
    }

    /**
     * 根据id获取航线
     * @param getAirlineByIdRequest
     * @return
     */
    @PostMapping("/getPlanarAirlineById")
    public R<UpdatePlanarAirlineResponseDTO> getPlanarAirlineById(@Validated @RequestBody GetAirlineByIdRequest getAirlineByIdRequest) {
        GetAirlineByIdRequestDTO getAirlineByIdRequestDTO = BeanUtil.copyProperties(getAirlineByIdRequest, GetAirlineByIdRequestDTO.class);
        UpdatePlanarAirlineResponseDTO updatePlanarAirlineResponseDTO = airlineService.getPlanarAirlineById(getAirlineByIdRequestDTO);
        return R.ok(updatePlanarAirlineResponseDTO);
    }

    /**
     * 根据id获取航线
     *
     * @param getAirlineByAirlineCodeRequest
     * @return
     */
    @PostMapping("/getAirlineByAirlineCode")
    public R<GetAirlineByAirlineCodeResponse> getAirlineByAirlineCode(@Validated @RequestBody GetAirlineByAirlineCodeRequest getAirlineByAirlineCodeRequest) {
        GetAirlineByAirlineCodeRequestDTO getAirlineByAirlineCodeRequestDTO = BeanUtil.copyProperties(getAirlineByAirlineCodeRequest, GetAirlineByAirlineCodeRequestDTO.class);
        GetAirlineByAirlineCodeResponseDTO getAirlineByAirlineCodeResponseDTO = airlineService.getAirlineByAirlineCode(getAirlineByAirlineCodeRequestDTO);
        return R.ok(BeanUtil.copyProperties(getAirlineByAirlineCodeResponseDTO, GetAirlineByAirlineCodeResponse.class));
    }

    /**
     * 删除航线
     *
     * @param id
     * @return
     */
//    @SaCheckPermission("航线管理")
    @PostMapping("/deleteAirline")
    public R<Void> deleteAirline(@RequestBody Long id) {
//        DeleteAirlineRequestDTO deleteAirlineRequestDTO = BeanUtil.copyProperties(deleteAirlineRequest, DeleteAirlineRequestDTO.class);
        Boolean result = airlineService.deleteAirline(id);
        if (result) {

            return R.ok();
        } else {
            return R.fail();
        }

    }

    @PostMapping("/deleteAirlineByOrganizationId")
    public R<Void> deleteAirlineByOrganizationId(@RequestBody Long id) {
//        DeleteAirlineRequestDTO deleteAirlineRequestDTO = BeanUtil.copyProperties(deleteAirlineRequest, DeleteAirlineRequestDTO.class);
        Boolean result = airlineService.deleteAirlineByOrganizationId(id);
        if (result) {

            return R.ok();
        } else {
            return R.fail();
        }


    }

    @PostMapping("/zbzh")
    public R<ZbzhResponse> zbzh(@RequestBody ZbzhRequest zbzhRequest) {
        CoordinateTransformUtil.Coordinate coordinate = null;
        ZbzhResponse zbzhResponse = new ZbzhResponse();
        if (zbzhRequest.getMethod().equals("gcj02ToWgs84")) {//高德转Wgs84
            coordinate = CoordinateTransformUtil.gcj02ToWgs84(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("gcj02ToBd09")) {//高德转百度
            coordinate = CoordinateTransformUtil.gcj02ToBd09(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("gcj02ToCgcs2000")) {//高德转大地2000
            //先转Wgs84
            CoordinateTransformUtil.Coordinate coordinate1 = CoordinateTransformUtil.gcj02ToWgs84(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
            //wgs84转大地2000
            coordinate = CoordinateTransformUtil.wgs84ToCgcs2000(coordinate1.getLongitude(), coordinate1.getLatitude());
        } else if (zbzhRequest.getMethod().equals("wgs84ToCgcs2000")) { //Wgs84转大地2000
            coordinate = CoordinateTransformUtil.wgs84ToCgcs2000(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("wgs84ToBd09")) {//wgs84转百度
            coordinate = CoordinateTransformUtil.wgs84ToBd09(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("wgs84ToGcj02")) {//wgs84转高德
            coordinate = CoordinateTransformUtil.wgs84ToGcj02(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("bd09ToGcj02")) {//百度转高德
            coordinate = CoordinateTransformUtil.bd09ToGcj02(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
        } else if (zbzhRequest.getMethod().equals("cgcs2000ToGcj02")) {//大地2000转高德
            //大地2000先转wgs84
            CoordinateTransformUtil.Coordinate coordinate1 = CoordinateTransformUtil.cgcs2000ToWgs84(zbzhRequest.getLongitude(), zbzhRequest.getLatitude());
            //wgs84转高德
            coordinate = CoordinateTransformUtil.wgs84ToGcj02(coordinate1.getLongitude(), coordinate1.getLatitude());
        }
        zbzhResponse.setLatitude(coordinate.getLatitude());
        zbzhResponse.setLongitude(coordinate.getLongitude());
        return R.ok(zbzhResponse);
    }

    /**
     * 获取品牌列表
     * @return
     */
    @PostMapping("/getBrandList")
    public R<List<GetBrandListResponseDTO>> getBrandList() {
         List<GetBrandListResponseDTO> result = modelService.getBrandList();
         return R.ok(result);
    }

    /**
     * 获取型号列表
     * @return
     */
    @PostMapping("/getModelList")
    public R<List<GetSeriesListResponseDTO>> getModelList(@RequestBody Map map) {
        if(map.containsKey("brandId")){
            Long brandId = Long.parseLong(map.get("brandId").toString());
            List<GetSeriesListResponseDTO> result = modelService.getModelList(brandId);
            return R.ok(result);
        }else{
            return R.fail();
        }

    }

    /**
     * 获取型号列表
     * @return
     */
    @PostMapping("/getModelList2")
    public R<List<FeisuanDeviceModelResponseDTO>> getModelList2(@RequestBody Map map) {
//        if(map.containsKey("brandId")){
//            Long brandId = Long.parseLong(map.get("brandId").toString());
            List<FeisuanDeviceModelResponseDTO> result = modelService.getModelList2(map);
            return R.ok(result);
//        }else{
//            return R.fail();
//        }

    }

}
